package se.teamstat.client.gui;

import java.util.List;

import se.teamstat.client.dto.EntityDTO;
import se.teamstat.client.gui.admin.AdminPanel;
import se.teamstat.client.gui.admin.EntityPanel;
import se.teamstat.client.gui.admin.ViewPanel;
import se.teamstat.client.gui.admin.matchserie.MatchSeriePanel;
import se.teamstat.client.gui.admin.player.PlayerPanel;
import se.teamstat.client.gui.admin.team.TeamPanel;
import se.teamstat.client.service.MainServiceAsync;
import se.teamstat.client.service.ServiceFactory;
import se.teamstat.client.service.ServiceFactoryImpl;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.layout.CardLayout;

public class CenterPanel extends Panel {
	
	private ServiceFactory serviceFactory;

	public CenterPanel() {
		init();
	}

	private void init() {
		serviceFactory = new ServiceFactoryImpl();
		setBodyStyle("background-color:#BCBCBC");
		setLayout(new CardLayout());
		setActiveItemID("admin-item");
		
		Panel adminPanel = createAdminPanel();
		Panel viewPanel = createViewPanel();
		
		//GridWidget grid = new GridWidget();
		//grid.update(PlayerDTO.class);
		
		//playersPanel.add(grid);
		add(adminPanel);
		add(viewPanel);
	}

	public Panel getMainPanel() {
		Panel panel = new Panel();
		panel.setBorder(false);

		final Panel wizardPanel = new Panel();
		wizardPanel.setLayout(new CardLayout());
		wizardPanel.setActiveItemID("forms");

		Panel first = new Panel();
		first.setBorder(false);
		first.setId("card-1");
		first.setHtml("<h1>Welcome to the Wizard!</h1><p>Step 1 of 3</p>");

		Panel formsPanel = createEntityPanel();
		formsPanel.setId("forms");
		formsPanel.setAutoWidth(true);
		formsPanel.setAutoHeight(true);

		wizardPanel.add(first);
		wizardPanel.add(formsPanel);

		return wizardPanel;
	}

	private Panel createAdminPanel() {
		Panel adminPanel = AdminPanel.createPanel();
		return adminPanel;
	}
	
	private Panel createViewPanel() {
		Panel viewPanel = ViewPanel.createPanel();
		return viewPanel;
	}
	
	private Panel createEntityPanel() {
		Panel panel = new Panel();
		panel.setBorder(false);

		FormPanel formPanel = new FormPanel();
		formPanel.setFrame(true);
		formPanel.setLabelWidth(75);
		formPanel.setBorder(false);

		final TabPanel tabPanel = new TabPanel();
		tabPanel.setActiveTab(0);

		Panel playerPanel = PlayerPanel.createPanel();
		Panel teamPanel = TeamPanel.createPanel();
		Panel matchSeriePanel = MatchSeriePanel.createPanel();
		tabPanel.add(playerPanel);
		tabPanel.add(teamPanel);
		tabPanel.add(matchSeriePanel);

		final AsyncCallback<EntityDTO> callback = new AsyncCallback<EntityDTO>() {
			public void onSuccess(EntityDTO entity) {
				MessageBox.alert("success:" + entity);
			}

			public void onFailure(Throwable caught) {
				MessageBox.alert("failed");
			}
		};

		final AsyncCallback<List<EntityDTO>> callbackList = new AsyncCallback<List<EntityDTO>>() {
			public void onSuccess(List<EntityDTO> entityList) {
				MessageBox.alert("success:" + entityList);
			}

			public void onFailure(Throwable caught) {
				MessageBox.alert("failed");
			}
		};

		ButtonListenerAdapter listener = new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				EntityPanel activePanel = (EntityPanel) tabPanel.getActiveTab();
				EntityDTO entity = activePanel.getEntity();
				getService(entity.getClass()).save(entity, callback);
			}
		};

		ButtonListenerAdapter listenerCancel = new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				EntityPanel activePanel = (EntityPanel) tabPanel.getActiveTab();
				EntityDTO entity = activePanel.getEntity();
				getService(entity.getClass()).getAll(callbackList);
			}
		};

		Button saveButton = new Button("Save", listener);
		Button cancelButton = new Button("Cancel", listenerCancel);

		formPanel.addButton(cancelButton);
		formPanel.addButton(saveButton);

		formPanel.add(tabPanel);
		panel.add(formPanel);

		return panel;
	}

	public MainServiceAsync<EntityDTO> getService(Class<? extends EntityDTO> entity) {
		return serviceFactory.getService(entity);
	}
}
